package com.itheima.controller;

import com.itheima.pojo.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/findAll")
    public String findAll(){
        return "findAll";
    }

    @RequestMapping("/commonParam")
    public String commonParam(@RequestParam("name") String name , int age){
        System.out.println("name = " + name);
        System.out.println("age = " + age);
        return "name ==>" + name + "|| age ==>" + age;
    }

    @RequestMapping("/commonParamDifferentName")
    public String commonParamDifferentName(@RequestParam("name") String username , int age){
        System.out.println("name = " + username);
        System.out.println("age = " + age);
        return "{'module':'common param different name'}";
    }

    @RequestMapping("/pojoParam")
    public String pojoParam(User user){
        System.out.println("pojo参数传递 user ==> "+user);
        return "{'module':'pojo param'}";
    }

    @RequestMapping("/arrayParam")
    public String arrayParam(String[] likes){
        System.out.println("数组参数传递 likes ==> "+ Arrays.toString(likes));
        return "{'module':'array param'}";
    }

    @RequestMapping("/listParam")
    public String listParam(@RequestParam List<String> likes){
        System.out.println("集合参数传递 likes ==> "+ likes);
        return "{'module':'list param'}";
    }

    @RequestMapping("/listParamForJson")
    public String listParamForJson(@RequestBody List<String> likes){
        System.out.println("list common(json)参数传递 list ==> "+likes);
        return "{'module':'list common for json param'}";
    }

    @RequestMapping("/listPojoParamForJson")
    public String listPojoParamForJson(@RequestBody List<User> list){
        System.out.println("list pojo(json)参数传递 list ==> "+list);
        return "{'module':'list pojo for json param'}";
    }

    @RequestMapping("/toJsonPOJO")
    public User toJsonPOJO(){
        User user = new User();
        user.setName("张三");
        user.setAge(21);
        return user;
    }
    @RequestMapping("/toJsonList")
    public List<User> toJsonList(){
        User user1 = new User();
        user1.setName("张三");
        user1.setAge(21);

        User user2 = new User();
        user2.setName("lisi");
        user2.setAge(23);

        List<User> users = new ArrayList<>();
        users.add(user1);
        users.add(user2);
        return users;
    }

}
